home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 002 / dbug / user.t < prev    next >
Text File  |  1995-03-17  |  42KB  |  1,349 lines

  1.  
  2.  
  3.  
  4.                                  D B U G
  5.  
  6.                        C Program Debugging Package
  7.  
  8.                                     by
  9.  
  10.                                 Fred Fish
  11.  
  12.  
  13.  
  14.  
  15.        IIIINNNNTTTTRRRROOOODDDDUUUUCCCCTTTTIIIIOOOONNNN
  16.  
  17.  
  18.             Almost every program development environment worthy  of
  19.        the  name provides some sort of debugging facility.  Usually
  20.        this takes the  form  of  a  program  which  is  capable  of
  21.        controlling  execution  of  other programs and examining the
  22.        internal state of other executing programs.  These types  of
  23.        programs will be referred to as external debuggers since the
  24.        debugger is not part of the executing program.  Examples  of
  25.        this  type  of  debugger  include  the aaaaddddbbbb and ssssddddbbbb debuggers
  26.        provided with the UUUUNNNNIIIIXXXX111 operating system.
  27.  
  28.  
  29.             One of the problems associated with developing programs
  30.        in  an  environment  with  good  external  debuggers is that
  31.        developed programs  tend  to  have  little  or  no  internal
  32.        instrumentation.   This  is  usually  not  a problem for the
  33.        developer since he is, or at  least  should  be,  intimately
  34.        familiar  with  the  internal organization, data structures,
  35.        and control flow of the program being  debugged.   It  is  a
  36.        serious   problem   for  maintenance  programmers,  who  are
  37.        unlikely to have such familiarity  with  the  program  being
  38.        maintained,  modified, or ported to another environment.  It
  39.        is also a problem, even for the developer, when the  program
  40.        is  moved  to  an environment with a primitive or unfamiliar
  41.        debugger, or even no debugger.
  42.  
  43.  
  44.             On the other hand, _d_b_u_g is an example  of  an  internal
  45.        debugger.  Because it requires internal instrumentation of a
  46.        program, and its  usage  does  not  depend  on  any  special
  47.        capabilities  of  the  execution  environment,  it is always
  48.        available and will  execute  in  any  environment  that  the
  49.        program  itself will execute in.  In addition, since it is a
  50.        complete  package  with  a  specific  user  interface,   all
  51.        programs   which  use  it  will  be  provided  with  similar
  52.        debugging capabilities.  This is in sharp contrast to  other
  53.  
  54.  
  55.        __________
  56.  
  57.         1. UNIX is a trademark of AT&T Bell Laboratories.
  58.  
  59.  
  60.  
  61.  
  62.                                   - 1 -
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.        DBUG User Manual       (preliminary)       December 20, 1985
  71.  
  72.  
  73.  
  74.        forms of internal instrumentation where each  developer  has
  75.        their  own, usually less capable, form of internal debugger.
  76.        In summary, because _d_b_u_g is an internal debugger it provides
  77.        consistency across operating environments, and because it is
  78.        available to all developers it provides  consistency  across
  79.        all programs in the same environment.
  80.  
  81.  
  82.             The _d_b_u_g package imposes only a slight speed penalty on
  83.        executing programs, typically much less than 10 percent, and
  84.        a modest size penalty,  typically  10  to  20  percent.   By
  85.        defining  a specific C preprocessor symbol both of these can
  86.        be reduced to zero with no changes required  to  the  source
  87.        code.
  88.  
  89.  
  90.             The  following  list  is  a  quick   summary   of   the
  91.        capabilities  of  the  _d_b_u_g package.  Each capability can be
  92.        individually enabled or disabled at the time  a  program  is
  93.        invoked   by   specifying   the   appropriate  command  line
  94.        arguments.
  95.  
  96.                o Execution trace  showing  function  level  control
  97.                  flow    in   a   semi-graphically   manner   using
  98.                  indentation to indicate nesting depth.
  99.  
  100.                o Output the values of all, or any  subset  of,  key
  101.                  internal variables.
  102.  
  103.                o Limit  actions  to  a  specific   set   of   named
  104.                  functions.
  105.  
  106.                o Limit function trace to a specified nesting depth.
  107.  
  108.                o Label each output line with source file  name  and
  109.                  line number.
  110.  
  111.                o Label  each  output  line  with  name  of  current
  112.                  process.
  113.  
  114.                o Push or pop  internal  debugging  state  to  allow
  115.                  execution with built in debugging defaults.
  116.  
  117.                o Redirect  the  debug  output  stream  to  standard
  118.                  output  (stdout)  or  a  named  file.  The default
  119.                  output stream is  standard  error  (stderr).   The
  120.                  redirection mechanism is completely independent of
  121.                  normal command line redirection  to  avoid  output
  122.                  conflicts.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.                                   - 2 -
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.        DBUG User Manual       (preliminary)       December 20, 1985
  137.  
  138.  
  139.  
  140.        PPPPRRRRIIIIMMMMIIIITTTTIIIIVVVVEEEE DDDDEEEEBBBBUUUUGGGGGGGGIIIINNNNGGGG TTTTEEEECCCCHHHHNNNNIIIIQQQQUUUUEEEESSSS
  141.  
  142.  
  143.             Internal instrumentation is already a familiar  concept
  144.        to most programmers, since it is usually the first debugging
  145.        technique  learned.    Typically,   "print statements"   are
  146.        inserted  in the source code at interesting points, the code
  147.        is recompiled and executed,  and  the  resulting  output  is
  148.        examined in an attempt to determine where the problem is.
  149.  
  150.        The procedure is iterative,  with  each  iteration  yielding
  151.        more  and  more  output,  and  hopefully  the  source of the
  152.        problem is discovered before the output becomes too large to
  153.        deal  with  or  previously  inserted  statements  need to be
  154.        removed.  Figure 1 is an example of this type  of  primitive
  155.        debugging technique.
  156.  
  157.  
  158.  
  159.                  #include <stdio.h>
  160.  
  161.                  main (argc, argv)
  162.                  int argc;
  163.                  char *argv[];
  164.                  {
  165.                      printf ("argv[0] = %d\n", argv[0]);
  166.                      /*
  167.                       *  Rest of program
  168.                       */
  169.                      printf ("== done ==\n");
  170.                  }
  171.  
  172.  
  173.                                    Figure 1
  174.                          Primitive Debugging Technique
  175.  
  176.  
  177.  
  178.  
  179.  
  180.             Eventually,  and  usually  after   at   least   several
  181.        iterations,  the  problem  will  be found and corrected.  At
  182.        this point, the newly  inserted  print  statements  must  be
  183.        dealt  with.   One obvious solution is to simply delete them
  184.        all.  Beginners usually do this a few times until they  have
  185.        to  repeat  the entire process every time a new bug pops up.
  186.        The second most obvious solution is to somehow  disable  the
  187.        output,  either  through  the  source code comment facility,
  188.        creation of a debug variable to be switched on or off, or by
  189.        using  the  C  preprocessor.   Figure 2 is an example of all
  190.        three techniques.
  191.  
  192.  
  193.  
  194.                                   - 3 -
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.        DBUG User Manual       (preliminary)       December 20, 1985
  203.  
  204.  
  205.  
  206.  
  207.  
  208.                  #include <stdio.h>
  209.  
  210.                  int debug = 0;
  211.  
  212.                  main (argc, argv)
  213.                  int argc;
  214.                  char *argv[];
  215.                  {
  216.                      /* printf ("argv = %x\n", argv) */
  217.                      if (debug) printf ("argv[0] = %d\n", argv[0]);
  218.                      /*
  219.                       *  Rest of program
  220.                       */
  221.                  #ifdef DEBUG
  222.                      printf ("== done ==\n");
  223.                  #endif
  224.                  }
  225.  
  226.  
  227.                                    Figure 2
  228.                            Debug Disable Techniques
  229.  
  230.  
  231.  
  232.  
  233.  
  234.             Each technique has  its  advantages  and  disadvantages
  235.        with  respect  to  dynamic vs static activation, source code
  236.        overhead, recompilation requirements, ease of  use,  program
  237.        readability,  etc.   Overuse  of  the  preprocessor solution
  238.        quickly leads to problems with source code  readability  and
  239.        maintainability  when  multiple  ####iiiiffffddddeeeeffff  symbols  are  to be
  240.        defined or  undefined  based  on  specific  types  of  debug
  241.        desired.  The source code can be made slightly more readable
  242.        by suitable indentation of the ####iiiiffffddddeeeeffff arguments to match the
  243.        indentation  of  the code, but not all C preprocessors allow
  244.        this.   The  only  requirement  for  the  standard  UUUUNNNNIIIIXXXX   C
  245.        preprocessor is for the '#' character to appear in the first
  246.        column,  but  even  this  seems  like   an   arbitrary   and
  247.        unreasonable  restriction.   Figure  3 is an example of this
  248.        usage.
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.                                   - 4 -
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.        DBUG User Manual       (preliminary)       December 20, 1985
  269.  
  270.  
  271.  
  272.  
  273.  
  274.                  #include <stdio.h>
  275.  
  276.                  main (argc, argv)
  277.                  int argc;
  278.                  char *argv[];
  279.                  {
  280.                  #   ifdef DEBUG
  281.                      printf ("argv[0] = %d\n", argv[0]);
  282.                  #   endif
  283.                      /*
  284.                       *  Rest of program
  285.                       */
  286.                  #   ifdef DEBUG
  287.                      printf ("== done ==\n");
  288.                  #   endif
  289.                  }
  290.  
  291.  
  292.                                    Figure 3
  293.                        More Readable Preprocessor Usage
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.                                   - 5 -
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.        DBUG User Manual       (preliminary)       December 20, 1985
  335.  
  336.  
  337.  
  338.        FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNN TTTTRRRRAAAACCCCEEEE EEEEXXXXAAAAMMMMPPPPLLLLEEEE
  339.  
  340.  
  341.             We will start off learning about  the  capabilities  of
  342.        the  _d_b_u_g  package  by  using  a simple minded program which
  343.        computes the factorial of a  number.   In  order  to  better
  344.        demonstrate  the  function  trace mechanism, this program is
  345.        implemented recursively.  Figure 4 is the main function  for
  346.        this factorial program.
  347.  
  348.  
  349.  
  350.                  #include <stdio.h>
  351.                  /* User programs should use <local/dbug.h> */
  352.                  #include "dbug.h"
  353.  
  354.                  main (argc, argv)
  355.                  int argc;
  356.                  char *argv[];
  357.                  {
  358.                      register int result, ix;
  359.                      extern int factorial (), atoi ();
  360.  
  361.                      DBUG_ENTER ("main");
  362.                      DBUG_PROCESS (argv[0]);
  363.                      for (ix = 1; ix < argc && argv[ix][0] == '-'; ix++) {
  364.                          switch (argv[ix][1]) {
  365.                              case '#':
  366.                                  DBUG_PUSH (&(argv[ix][2]));
  367.                                  break;
  368.                          }
  369.                      }
  370.                      for (; ix < argc; ix++) {
  371.                          DBUG_4 ("args", "argv[%d] = %s", ix, argv[ix]);
  372.                          result = factorial (atoi (argv[ix]));
  373.                          printf ("%d\n", result);
  374.                      }
  375.                      DBUG_RETURN (0);
  376.                  }
  377.  
  378.  
  379.                                    Figure 4
  380.                           Factorial Program Mainline
  381.  
  382.  
  383.  
  384.  
  385.  
  386.             The mmmmaaaaiiiinnnn function is  responsible  for  processing  any
  387.        command   line  option  arguments  and  then  computing  and
  388.        printing the factorial of each non-option argument.
  389.  
  390.  
  391.  
  392.                                   - 6 -
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.        DBUG User Manual       (preliminary)       December 20, 1985
  401.  
  402.  
  403.  
  404.             First of all, notice that all of the debugger functions
  405.        are  implemented  via  preprocessor  macros.   This does not
  406.        detract from the readability of the code and makes disabling
  407.        all debug compilation trivial (a single preprocessor symbol,
  408.        DDDDBBBBUUUUGGGG____OOOOFFFFFFFF, forces the macro expansions to be null).
  409.  
  410.             Also notice the inclusion of  the  header  file  ddddbbbbuuuugggg....hhhh
  411.        from the local header file directory.  (The version included
  412.        here is the test version in  the  dbug  source  distribution
  413.        directory).   This file contains all the definitions for the
  414.        debugger macros, which all have the form DDDDBBBBUUUUGGGG____XXXXXXXX............XXXXXXXX.
  415.  
  416.  
  417.             The DDDDBBBBUUUUGGGG____EEEENNNNTTTTEEEERRRR macro informs that debugger that we have
  418.        entered  the function named mmmmaaaaiiiinnnn.  It must be the very first
  419.        "executable" line in a function, after all declarations  and
  420.        before any other executable line.  The DDDDBBBBUUUUGGGG____PPPPRRRROOOOCCCCEEEESSSSSSSS macro is
  421.        generally used only once per program to inform the  debugger
  422.        what name the program was invoked with.  The DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH macro
  423.        modifies the current debugger state by saving  the  previous
  424.        state  and  setting  a new state based on the control string
  425.        passed as its argument.  The DDDDBBBBUUUUGGGG____4444 macro is used  to  print
  426.        the  values  of each argument for which a factorial is to be
  427.        computed.  The DDDDBBBBUUUUGGGG____RRRREEEETTTTUUUURRRRNNNN macro tells the debugger that the
  428.        end  of  the current function has been reached and returns a
  429.        value to the calling function.  All of these macros will  be
  430.        fully explained in subsequent sections.
  431.  
  432.             To use the debugger, the factorial program  is  invoked
  433.        with a command line of the form:
  434.  
  435.                           factorial -#d:t 1 2 3
  436.  
  437.        The  mmmmaaaaiiiinnnn  function  recognizes  the  "-#d:t"  string  as  a
  438.        debugger  control  string, and passes the debugger arguments
  439.        ("d:t")  to  the  _d_b_u_g  runtime  support  routines  via  the
  440.        DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH macro.  This particular string enables output from
  441.        the DDDDBBBBUUUUGGGG____4444 macro with the  'd'  flag  and  enables  function
  442.        tracing  with  the 't' flag.  The factorial function is then
  443.        called three times, with the arguments "1", "2", and "3".
  444.  
  445.  
  446.             Debug control strings consist of a  header,  the  "-#",
  447.        followed  by  a  colon separated list of debugger arguments.
  448.        Each debugger argument is a single character  flag  followed
  449.        by  an optional comma separated list of argments specific to
  450.        the given flag.  Some examples are:
  451.  
  452.                           -#d:t:o
  453.                           -#d,in,out:f,main:F:L
  454.  
  455.  
  456.  
  457.  
  458.                                   - 7 -
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.        DBUG User Manual       (preliminary)       December 20, 1985
  467.  
  468.  
  469.  
  470.        Note  that  previously  enabled  debugger  actions  can   be
  471.        disabled by the control string "-#".
  472.  
  473.  
  474.             The definition of the factorial function, symbolized as
  475.        "N!", is given by:
  476.  
  477.                          N! = N * N-1 * ... 2 * 1
  478.  
  479.        Figure 5 is the factorial  function  which  implements  this
  480.        algorithm  recursively.   Note  that this is not necessarily
  481.        the best way to  do  factorials  and  error  conditions  are
  482.        ignored completely.
  483.  
  484.  
  485.  
  486.                  #include <stdio.h>
  487.                  /* User programs should use <local/dbug.h> */
  488.                  #include "dbug.h"
  489.  
  490.                  int factorial (value)
  491.                  register int value;
  492.                  {
  493.                      DBUG_ENTER ("factorial");
  494.                      DBUG_3 ("find", "find %d factorial", value);
  495.                      if (value > 1) {
  496.                          value *= factorial (value - 1);
  497.                      }
  498.                      DBUG_3 ("result", "result is %d", value);
  499.                      DBUG_RETURN (value);
  500.                  }
  501.  
  502.  
  503.                                    Figure 5
  504.                               Factorial Function
  505.  
  506.  
  507.  
  508.  
  509.  
  510.             One advantage (some may not consider it  so)  to  using
  511.        the  _d_b_u_g  package  is  that  it  strongly  encourages fully
  512.        structured coding with only one entry and one exit point  in
  513.        each  function.  Multiple exit points, such as early returns
  514.        to escape a loop, may be used, but each such point  requires
  515.        the  use  of  an appropriate DDDDBBBBUUUUGGGG____RRRREEEETTTTUUUURRRRNNNN or DDDDBBBBUUUUGGGG____VVVVOOOOIIIIDDDD____RRRREEEETTTTUUUURRRRNNNN
  516.        macro.
  517.  
  518.  
  519.             To build  the  factorial  program  on  a  UUUUNNNNIIIIXXXX  system,
  520.        compile and link with the command:
  521.  
  522.  
  523.  
  524.                                   - 8 -
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.        DBUG User Manual       (preliminary)       December 20, 1985
  533.  
  534.  
  535.  
  536.                 cc -o factorial main.c factorial.c -ldbug
  537.  
  538.        The "-ldbug" argument  tells  the  loader  to  link  in  the
  539.        runtime support modules for the _d_b_u_g package.  Executing the
  540.        factorial program with a command of the form:
  541.  
  542.                            factorial 1 2 3 4 5
  543.  
  544.        generates the output shown in figure 6.
  545.  
  546.  
  547.  
  548.                  1
  549.                  2
  550.                  6
  551.                  24
  552.                  120
  553.  
  554.  
  555.                                    Figure 6
  556.                               factorial 1 2 3 4 5
  557.  
  558.  
  559.  
  560.  
  561.  
  562.             Function  level  tracing  is  enabled  by  passing  the
  563.        debugger the 't' flag in the debug control string.  Figure 7
  564.        is  the  output  resulting  from  the  command  "factorial -
  565.        #t:o 3 2".
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.                                   - 9 -
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.        DBUG User Manual       (preliminary)       December 20, 1985
  599.  
  600.  
  601.  
  602.  
  603.  
  604.                  |   >factorial
  605.                  |   |   >factorial
  606.                  |   |   <factorial
  607.                  |   <factorial
  608.                  2
  609.                  |   >factorial
  610.                  |   |   >factorial
  611.                  |   |   |   >factorial
  612.                  |   |   |   <factorial
  613.                  |   |   <factorial
  614.                  |   <factorial
  615.                  6
  616.                  <main
  617.  
  618.  
  619.                                    Figure 7
  620.                               factorial -#t:o 3 2
  621.  
  622.  
  623.  
  624.  
  625.  
  626.             Each entry to or return from a function is indicated by
  627.        '>'  for  the  entry  point  and  '<'  for  the  exit point,
  628.        connected by vertical bars to allow matching  points  to  be
  629.        easily found when separated by large distances.
  630.  
  631.  
  632.             This trace output indicates that there was  an  initial
  633.        call  to  factorial from main (to compute 2!), followed by a
  634.        single recursive call to factorial to compute 1!.  The  main
  635.        program  then  output  the  result  for  2!  and  called the
  636.        factorial  function  again  with  the  second  argument,  3.
  637.        Factorial  called  itself  recursively to compute 2! and 1!,
  638.        then returned control to main, which output the value for 3!
  639.        and exited.
  640.  
  641.  
  642.             Note that there is no matching entry point "main>"  for
  643.        the  return point "<main" because at the time the DDDDBBBBUUUUGGGG____EEEENNNNTTTTEEEERRRR
  644.        macro was reached in main, tracing was not enabled yet.   It
  645.        was  only  after  the  macro  DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH  was  executing that
  646.        tracing became enabled.  This implies that the argument list
  647.        should  be  processed  as  early  as possible since all code
  648.        preceding  the  first  call  to  DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH  is   essentially
  649.        invisible  to  ddddbbbbuuuugggg (this can be worked around by inserted a
  650.        temporary   DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH((((aaaarrrrggggvvvv[[[[1111]]]]))))   immediately    after    the
  651.        DDDDBBBBUUUUGGGG____EEEENNNNTTTTEEEERRRR((((""""mmmmaaaaiiiinnnn"""")))) macro.
  652.  
  653.  
  654.  
  655.  
  656.                                   - 10 -
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.        DBUG User Manual       (preliminary)       December 20, 1985
  665.  
  666.  
  667.  
  668.             One last note, the trace output normally comes  out  on
  669.        the  standard error.  Since the factorial program prints its
  670.        result on the standard output, there is the  possibility  of
  671.        the  output  on  the  terminal  being  scrambled  if the two
  672.        streams are not synchronized.  Thus the debugger is told  to
  673.        write its output on the standard output instead, via the 'o'
  674.        flag character.   Note  that  no  'o'  implies  the  default
  675.        (standard  error),  a  'o'  with no arguments means standard
  676.        output, and a 'o' with an  argument  means  used  the  named
  677.        file.   I.E,  "factorial -#t:o,logfile 3 2"  would write the
  678.        trace output in "logfile".  Because of  UUUUNNNNIIIIXXXX  implementation
  679.        details,  programs usually run faster when writing to stdout
  680.        rather than stderr, though this is not a prime consideration
  681.        in this example.
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.                                   - 11 -
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.        DBUG User Manual       (preliminary)       December 20, 1985
  731.  
  732.  
  733.  
  734.        UUUUSSSSEEEE OOOOFFFF DDDDBBBBUUUUGGGG____NNNN MMMMAAAACCCCRRRROOOOSSSS
  735.  
  736.  
  737.             The mechanism used to produce "printf" style output  is
  738.        the DDDDBBBBUUUUGGGG____NNNN macro, where NNNN is replaced by the matching number
  739.        of arguments to the macro.  Unfortunately, the standard UUUUNNNNIIIIXXXX
  740.        C preprocessor does not allow macros to have variable number
  741.        of arguments.  If it did,  then  a  single  macro  (such  as
  742.        DBUG_PRINTF) could replace all the DDDDBBBBUUUUGGGG____NNNN macros.
  743.  
  744.  
  745.             To allow selection of output from specific macros,  the
  746.        first  argument  to  every  DDDDBBBBUUUUGGGG____NNNN  macro is a _d_b_u_g keyword.
  747.        When this keyword appears in the argument list  of  the  'd'
  748.        flag    in    a    debug    control   string,   as   in   "-
  749.        #d,keyword1,keyword2,...:t", output from  the  corresponding
  750.        macro  is enabled.  The default when there is no 'd' flag in
  751.        the control string is  to  enable  output  from  all  DDDDBBBBUUUUGGGG____NNNN
  752.        macros.
  753.  
  754.  
  755.             Typically, a program will be run once, with no keywords
  756.        specified,  to  determine  what keywords are significant for
  757.        the current problem (the keywords are printed in  the  macro
  758.        output  line).  Then the program will be run again, with the
  759.        desired  keywords,  to  examine  only  specific   areas   of
  760.        interest.
  761.  
  762.  
  763.             The rest of the argument list to a DDDDBBBBUUUUGGGG____NNNN is a standard
  764.        printf  style  format  string  and  one or more arguments to
  765.        print.  Note that no explicit newline is required at the end
  766.        of  the  format  string.  As a matter of style, two or three
  767.        small DDDDBBBBUUUUGGGG____NNNN macros are preferable to a single macro with  a
  768.        huge  format  string.  Figure 8 shows the output for default
  769.        tracing and debug.
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.                                   - 12 -
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.        DBUG User Manual       (preliminary)       December 20, 1985
  797.  
  798.  
  799.  
  800.  
  801.  
  802.                  |   args: argv[2] = 3
  803.                  |   >factorial
  804.                  |   |   find: find 3 factorial
  805.                  |   |   >factorial
  806.                  |   |   |   find: find 2 factorial
  807.                  |   |   |   >factorial
  808.                  |   |   |   |   find: find 1 factorial
  809.                  |   |   |   |   result: result is 1
  810.                  |   |   |   <factorial
  811.                  |   |   |   result: result is 2
  812.                  |   |   <factorial
  813.                  |   |   result: result is 6
  814.                  |   <factorial
  815.                  6
  816.                  <main
  817.  
  818.  
  819.                                    Figure 8
  820.                               factorial -#d:t:o 3
  821.  
  822.  
  823.  
  824.  
  825.  
  826.             The output from the DDDDBBBBUUUUGGGG____NNNN macro is indented  to  match
  827.        the trace output for the function in which the macro occurs.
  828.        When debugging is enabled, but not trace, the output  starts
  829.        at the left margin, without indentation.
  830.  
  831.  
  832.             To demonstrate selection of specific macros for output,
  833.        figure  9  shows  the  result  when the factorial program is
  834.        invoked with the debug control string "-#d,result:o".
  835.  
  836.  
  837.  
  838.                  factorial: result: result is 1
  839.                  factorial: result: result is 2
  840.                  factorial: result: result is 6
  841.                  factorial: result: result is 24
  842.                  24
  843.  
  844.  
  845.                                    Figure 9
  846.                            factorial -#d,result:o 4
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.                                   - 13 -
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.        DBUG User Manual       (preliminary)       December 20, 1985
  863.  
  864.  
  865.  
  866.             It is sometimes desirable  to  restrict  debugging  and
  867.        trace  actions  to a specific function or list of functions.
  868.        This is accomplished with the  'f'  flag  character  in  the
  869.        debug  control  string.   Figure  10  is  the  output of the
  870.        factorial program  when  run  with  the  control  string  "-
  871.        #d:f,factorial:F:L:o".  The 'F' flag enables printing of the
  872.        source file name and the 'L' flag enables  printing  of  the
  873.        source file line number.
  874.  
  875.  
  876.  
  877.                     factorial.c:     9: factorial: find: find 3 factorial
  878.                     factorial.c:     9: factorial: find: find 2 factorial
  879.                     factorial.c:     9: factorial: find: find 1 factorial
  880.                     factorial.c:    13: factorial: result: result is 1
  881.                     factorial.c:    13: factorial: result: result is 2
  882.                     factorial.c:    13: factorial: result: result is 6
  883.                  6
  884.  
  885.  
  886.                                    Figure 10
  887.                        factorial -#d:f,factorial:F:L:o 3
  888.  
  889.  
  890.  
  891.  
  892.  
  893.             The output in figure 10 shows that the "find" macro  is
  894.        in  file  "factorial.c"  at  source  line 8 and the "result"
  895.        macro is in the same file at source line 12.
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.                                   - 14 -
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.        DBUG User Manual       (preliminary)       December 20, 1985
  929.  
  930.  
  931.  
  932.        SSSSUUUUMMMMMMMMAAAARRRRYYYY OOOOFFFF MMMMAAAACCCCRRRROOOOSSSS
  933.  
  934.  
  935.             This section summarizes  the  usage  of  all  currently
  936.        defined  macros in the _d_b_u_g package.  The macros definitions
  937.        are found in the user include file ddddbbbbuuuugggg....hhhh from the  standard
  938.        include directory.
  939.  
  940.  
  941.  
  942.                DBUG_ENTER  Used to tell the runtime support  module
  943.                            the  name of the function being entered.
  944.                            The argument must be of type "pointer to
  945.                            character".   The  DBUG_ENTER macro must
  946.                            precede  all  executable  lines  in  the
  947.                            function  just  entered,  and  must come
  948.                            after  all  local  declarations.    Each
  949.                            DBUG_ENTER  macro  must  have a matching
  950.                            DBUG_RETURN or DBUG_VOID_RETURN macro at
  951.                            the  function  exit  points.  DBUG_ENTER
  952.                            macros   used   without    a    matching
  953.                            DBUG_RETURN  or  DBUG_VOID_RETURN  macro
  954.                            will cause  warning  messages  from  the
  955.                            _d_b_u_g package runtime support module.
  956.  
  957.                            EX: DBUG_ENTER ("main");
  958.  
  959.               DBUG_RETURN  Used at each exit point  of  a  function
  960.                            containing  a  DBUG_ENTER  macro  at the
  961.                            entry point.  The argument is the  value
  962.                            to  return.   Functions  which return no
  963.                            value    (void)    should    use     the
  964.                            DBUG_VOID_RETURN  macro.  It is an error
  965.                            to     have     a     DBUG_RETURN     or
  966.                            DBUG_VOID_RETURN  macro  in  a  function
  967.                            which has no matching DBUG_ENTER  macro,
  968.                            and  the  compiler  will complain if the
  969.                            macros are actually used (expanded).
  970.  
  971.                            EX: DBUG_RETURN (value);
  972.                            EX: DBUG_VOID_RETURN;
  973.  
  974.              DBUG_PROCESS  Used to name the current  process  being
  975.                            executed.   A  typical argument for this
  976.                            macro is "argv[0]", though  it  will  be
  977.                            perfectly happy with any other string.
  978.  
  979.                            EX: DBUG_PROCESS (argv[0]);
  980.  
  981.                 DBUG_PUSH  Sets a new debugger state by pushing the
  982.                            current  ddddbbbbuuuugggg  state  onto  an  internal
  983.  
  984.  
  985.  
  986.                                   - 15 -
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.        DBUG User Manual       (preliminary)       December 20, 1985
  995.  
  996.  
  997.  
  998.                            stack and setting up the new state using
  999.                            the  debug  control string passed as the
  1000.                            macro argument.  The most  common  usage
  1001.                            is to set the state specified by a debug
  1002.                            control  string   retrieved   from   the
  1003.                            argument  list.   Note  that the leading
  1004.                            "-#" in a debug control string specified
  1005.                            as  a  command line argument must nnnnooootttt be
  1006.                            passed as part of  the  macro  argument.
  1007.                            The proper usage is to pass a pointer to
  1008.                            the  first  character  aaaafffftttteeeerrrr  the   "-#"
  1009.                            string.
  1010.  
  1011.                            EX: DBUG_PUSH ((argv[i][2]));
  1012.                            EX: DBUG_PUSH ("d:t");
  1013.                            EX: DBUG_PUSH ("");
  1014.  
  1015.                  DBUG_POP  Restores the previous debugger state  by
  1016.                            popping  the state stack.  Attempting to
  1017.                            pop more  states  than  pushed  will  be
  1018.                            ignored  and  no  warning will be given.
  1019.                            The DBUG_POP macro has no arguments.
  1020.  
  1021.                            EX: DBUG_POP ();
  1022.  
  1023.                 DBUG_FILE  The  DBUG_FILE  macro  is  used  to   do
  1024.                            explicit I/O on the debug output stream.
  1025.                            It is used in the  same  manner  as  the
  1026.                            symbols  "stdout"  and  "stderr"  in the
  1027.                            standard I/O package.
  1028.  
  1029.                            EX: fprintf (DBUG_FILE, "Doing  my   own
  1030.                            I/O!0);
  1031.  
  1032.              DBUG_EXECUTE  The  DBUG_EXECUTE  macro  is   used   to
  1033.                            execute any arbitrary C code.  The first
  1034.                            argument is the debug keyword,  used  to
  1035.                            trigger  execution of the code specified
  1036.                            as the second argument.  This macro must
  1037.                            be  used  cautiously  because,  like the
  1038.                            DBUG_N  macros,  it   is   automatically
  1039.                            selected  by  default  whenever  the 'd'
  1040.                            flag has no argument list  (I.E.,  a  "-
  1041.                            #d:t" control string).
  1042.  
  1043.                            EX: DBUG_EXECUTE ("abort", abort ());
  1044.  
  1045.                    DBUG_N  Used to do printing  via  the  "fprintf"
  1046.                            library  function  on  the current debug
  1047.                            stream, DBUG_FILE.  N may currently be a
  1048.                            number  in  the range 2-5, and specifies
  1049.  
  1050.  
  1051.  
  1052.                                   - 16 -
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.        DBUG User Manual       (preliminary)       December 20, 1985
  1061.  
  1062.  
  1063.  
  1064.                            the   number   of   arguments   in   the
  1065.                            corresponding macro.  The first argument
  1066.                            is a debug  keyword,  the  second  is  a
  1067.                            format   string,   and   the   remaining
  1068.                            arguments, if any, are the values to  be
  1069.                            printed.
  1070.  
  1071.                            EX: DBUG_2 ("eof", "end of file found");
  1072.                            EX: DBUG_3 ("type","type is %x", type);
  1073.                            EX: DBUG_4 ("stp",   "%x -> %s",    stp,
  1074.                            stp -> name);
  1075.  
  1076.               DBUG_SETJMP  Used in place of the  setjmp()  function
  1077.                            to first save the current debugger state
  1078.                            and then  execute  the  standard  setjmp
  1079.                            call.   This  allows  to the debugger to
  1080.                            restore it's state when the DBUG_LONGJMP
  1081.                            macro  is  used  to  invoke the standard
  1082.                            longjmp() call.  Currently all instances
  1083.                            of  DBUG_SETJMP  must  occur  within the
  1084.                            same function and at the  same  function
  1085.                            nesting level.
  1086.  
  1087.                            EX: DBUG_SETJMP (env);
  1088.  
  1089.              DBUG_LONGJMP  Used in place of the longjmp()  function
  1090.                            to  first  restore the previous debugger
  1091.                            state  at   the   time   of   the   last
  1092.                            DBUG_SETJMP   and   then   execute   the
  1093.                            standard  longjmp()  call.   Note   that
  1094.                            currently    all   DBUG_LONGJMP   macros
  1095.                            restore the state at  the  time  of  the
  1096.                            last  DBUG_SETJMP.  It would be possible
  1097.                            to  maintain  separate  DBUG_SETJMP  and
  1098.                            DBUG_LONGJMP   pairs   by   having   the
  1099.                            debugger runtime support module use  the
  1100.                            first   argument  to  differentiate  the
  1101.                            pairs.
  1102.  
  1103.                            EX: DBUG_LONGJMP (env,val);
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.                                   - 17 -
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.        DBUG User Manual       (preliminary)       December 20, 1985
  1127.  
  1128.  
  1129.  
  1130.        DDDDEEEEBBBBUUUUGGGG CCCCOOOONNNNTTTTRRRROOOOLLLL SSSSTTTTRRRRIIIINNNNGGGG
  1131.  
  1132.  
  1133.             The debug control string is used to set  the  state  of
  1134.        the   debugger   via  the  DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH  macro.   This  section
  1135.        summarizes the currently available debugger options and  the
  1136.        flag  characters  which  enable  or  disable them.  Argument
  1137.        lists enclosed in '[' and ']' are optional.
  1138.  
  1139.  
  1140.                 d[,keywords] Enable   output   from   macros   with
  1141.                              specified  keywords.   A  null list of
  1142.                              keywords implies that all keywords are
  1143.                              selected.
  1144.  
  1145.                     D[,time] Delay for specified  time  after  each
  1146.                              output  line,  to  let  output  drain.
  1147.                              Time is given in tenths  of  a  second
  1148.                              (value  of 10 is one second).  Default
  1149.                              is zero.
  1150.  
  1151.                f[,functions] Limit   debugger   actions   to    the
  1152.                              specified  list  of functions.  A null
  1153.                              list of  functions  implies  that  all
  1154.                              functions are selected.
  1155.  
  1156.                            F Mark each debugger  output  line  with
  1157.                              the name of the source file containing
  1158.                              the macro causing the output.
  1159.  
  1160.                            L Mark each debugger  output  line  with
  1161.                              the  source  file  line  number of the
  1162.                              macro causing the output.
  1163.  
  1164.                            n Mark each debugger  output  line  with
  1165.                              the current function nesting depth.
  1166.  
  1167.                            N Sequentially  number   each   debugger
  1168.                              output  line  starting  at 1.  This is
  1169.                              useful  for  reference  purposes  when
  1170.                              debugger  output  is interspersed with
  1171.                              program output.
  1172.  
  1173.                     o[,file] Redirect the debugger output stream to
  1174.                              the   specified   file.   The  default
  1175.                              output  stream  is  stderr.   A   null
  1176.                              argument  list  causes  output  to  be
  1177.                              redirected to stdout.
  1178.  
  1179.                p[,processes] Limit   debugger   actions   to    the
  1180.                              specified   processes.   A  null  list
  1181.  
  1182.  
  1183.  
  1184.                                   - 18 -
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.        DBUG User Manual       (preliminary)       December 20, 1985
  1193.  
  1194.  
  1195.  
  1196.                              implies all processes.  This is useful
  1197.                              for    processes   which   run   child
  1198.                              processes.  Note  that  each  debugger
  1199.                              output  line  can  be  marked with the
  1200.                              name of the current  process  via  the
  1201.                              'P' flag.  The process name must match
  1202.                              the    argument    passed    to    the
  1203.                              DDDDBBBBUUUUGGGG____PPPPRRRROOOOCCCCEEEESSSSSSSS macro.
  1204.  
  1205.                            P Mark each debugger  output  line  with
  1206.                              the name of the current process.  Most
  1207.                              useful when used with a process  which
  1208.                              runs  child  processes  that  are also
  1209.                              being debugged.  Note that the  parent
  1210.                              process  must arrange for the debugger
  1211.                              control string to  be  passed  to  the
  1212.                              child processes.
  1213.  
  1214.                            r Used in conjunction with the DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH
  1215.                              macro to reset the current indentation
  1216.                              level back to zero.  Most useful  with
  1217.                              DDDDBBBBUUUUGGGG____PPPPUUUUSSSSHHHH  macros  used to temporarily
  1218.                              alter the debugger state.
  1219.  
  1220.                        t[,N] Enable function control flow  tracing.
  1221.                              The maximum nesting depth is specified
  1222.                              by N, and defaults to 200.
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.                                   - 19 -
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.        DBUG User Manual       (preliminary)       December 20, 1985
  1259.  
  1260.  
  1261.  
  1262.        HHHHIIIINNNNTTTTSSSS AAAANNNNDDDD MMMMIIIISSSSCCCCEEEELLLLLLLLAAAANNNNEEEEOOOOUUUUSSSS
  1263.  
  1264.  
  1265.             One of the most useful capabilities of the _d_b_u_g package
  1266.        is  to  compare  the  executions  of  a given program in two
  1267.        different environments.  This is typically done by executing
  1268.        the program in the environment where it behaves properly and
  1269.        saving the debugger output in a reference file.  The program
  1270.        is  then  run with identical inputs in the environment where
  1271.        it  misbehaves  and  the  output  is  again  captured  in  a
  1272.        reference  file.   The  two  reference  files  can  then  be
  1273.        differentially compared to determine exactly where execution
  1274.        of the two processes diverges.
  1275.  
  1276.  
  1277.             A  related  usage  is  regression  testing  where   the
  1278.        execution   of   a   current  version  is  compared  against
  1279.        executions of previous versions.  This is most  useful  when
  1280.        there are only minor changes.
  1281.  
  1282.  
  1283.             It is not difficult to modify an existing  compiler  to
  1284.        implement  some  of  the  functionality  of the _d_b_u_g package
  1285.        automatically, without source code changes  to  the  program
  1286.        being debugged.  In fact, such changes were implemented in a
  1287.        version of the Portable C Compiler by  the  author  in  less
  1288.        than  a  day.   However,  it is strongly encouraged that all
  1289.        newly developed code continue to use the debugger macros for
  1290.        the   portability   reasons  noted  earlier.   The  modified
  1291.        compiler should be used only for testing existing programs.
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.                                   - 20 -
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.        DBUG User Manual       (preliminary)       December 20, 1985
  1325.  
  1326.  
  1327.  
  1328.        CCCCAAAAVVVVEEEEAAAATTTTSSSS
  1329.  
  1330.  
  1331.             The _d_b_u_g package works best with  programs  which  have
  1332.        "line oriented"  output,  such  as  text processors, general
  1333.        purpose utilities, etc.  It can be  interfaced  with  screen
  1334.        oriented  programs  such as visual editors by redefining the
  1335.        appropriate macros to call special functions for  displaying
  1336.        the  debugger  results.   Of  course,  this  caveat  is  not
  1337.        applicable if the debugger output is simply  dumped  into  a
  1338.        file for post-execution examination.
  1339.  
  1340.  
  1341.             Programs which use memory  allocation  functions  other
  1342.        than  mmmmaaaalllllllloooocccc  will  usually have problems using the standard
  1343.        _d_b_u_g package.  The most common problem is multiply allocated
  1344.        memory.
  1345.  
  1346.  
  1347.        ********************
  1348.        ERROR:(user.r)input line 1065:DE:no DS or DF active
  1349.